package com.zabra.zl.ui.buy.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.DetailsDealBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.data.StockBean;
import com.zabra.zl.net.RxScheduler;
import com.zabra.zl.ui.buy.contract.BuyContract;
import com.zabra.zl.ui.buy.model.BuyModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BuyPresenter extends BasePresenter<BuyContract.View> implements BuyContract.Presenter {
    private BuyContract.Model model;

    public BuyPresenter() {
        model = new BuyModel();
    }

    @Override
    public void getData(String sharesCode, int bpOrderId) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(sharesCode, bpOrderId).compose(RxScheduler.<StockBean>Flo_io_main())
                .as(mView.<StockBean>bindAutoDispose())
                .subscribe(new Consumer<StockBean>() {
                    @Override
                    public void accept(StockBean bean) throws Exception {
                        mView.hideLoading();
                        mView.onSuccess(bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.onError(throwable);
                    }
                });
    }

    @Override
    public void getDataFresh(String sharesCode, int bpOrderId) {
        if (!isViewAttached()) {
            return;
        }
        model.getData(sharesCode, bpOrderId).compose(RxScheduler.<StockBean>Flo_io_main())
                .as(mView.<StockBean>bindAutoDispose())
                .subscribe(new Consumer<StockBean>() {
                    @Override
                    public void accept(StockBean bean) throws Exception {
                        mView.onSuccessFresh(bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                    }
                });
    }

    @Override
    public void getData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(body).enqueue(new Callback<DetailsDealBean>() {
            @Override
            public void onResponse(Call<DetailsDealBean> call, Response<DetailsDealBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<DetailsDealBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getData(String sharesCode, String sharesName, String entrustPrice, String entrustNum, String entrustProportion, String bpOrderId) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(sharesCode, sharesName, entrustPrice, entrustNum, entrustProportion, bpOrderId).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
